package cn.kooki.app.duobao.ui.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.util.ScreenUtils;

/**
 * Created by xiong on 2015/12/30.
 */
public class JSBridgeWebView extends WebView {
    private ProgressBar progressbar;
    private View netWorkErrorWarpper;

    public interface onRetryClickListener {
        void onRetry();
    }

    private onRetryClickListener retryClickListener;

    public void setRetryClickListener(onRetryClickListener retryClickListener) {
        this.retryClickListener = retryClickListener;
    }

    public JSBridgeWebView(Context context) {
        this(context, null);
    }

    public JSBridgeWebView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.webViewStyle);
    }

    public JSBridgeWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPorgress();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public JSBridgeWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initPorgress();
    }

    private void initPorgress() {
        progressbar = new ProgressBar(getContext(), null, android.R.attr.progressBarStyleHorizontal);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ScreenUtils.dpToPxInt(getContext(), 3));
        progressbar.setLayoutParams(layoutParams);
        progressbar.setProgressDrawable(getContext().getResources().getDrawable(R.drawable.webprogress));
        addView(progressbar);
        LayoutInflater.from(getContext()).inflate(R.layout.no_network, this, true);
        netWorkErrorWarpper = findViewById(R.id.network_error_warpper);
        netWorkErrorWarpper.setVisibility(GONE);
        //        setWebViewClient(new WebViewClient(){});
        setWebChromeClient(new WebChromeClient());
        setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

        });
    }

    public void showNetError() {
        if (netWorkErrorWarpper != null) {
            bringChildToFront(netWorkErrorWarpper);
            netWorkErrorWarpper.setVisibility(VISIBLE);
        }
    }

    public void hideNetError() {
        if (netWorkErrorWarpper != null) {
            netWorkErrorWarpper.setVisibility(GONE);
        }
    }

    public class WebChromeClient extends android.webkit.WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressbar.setVisibility(GONE);
            } else {
                if (progressbar.getVisibility() == GONE)
                    progressbar.setVisibility(VISIBLE);
                progressbar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        LayoutParams lp = (LayoutParams) progressbar.getLayoutParams();
        lp.x = l;
        lp.y = t;
        progressbar.setLayoutParams(lp);
        super.onScrollChanged(l, t, oldl, oldt);
    }


}
